<template>
  <div class="f-menu" :style="{ width: store.asideWidth }">
    <el-menu
      :default-active="defaultActive"
      class="border-0"
      :collapse="isCollapse"
      :collapse-transition="false"
      unique-opened
      @select="handleSelect"
    >
      <template v-for="(item, index) in asideMenus" :key="index">
        <el-sub-menu
          :index="item.label"
          v-if="item.children && item.children.length > 0"
        >
          <!-- 第一级别  -->
          <template #title>
            <el-icon>
              <component :is="item.meta.icon"></component>
            </el-icon>
            <span> {{ item.label }}</span>
          </template>
          <!-- 第二级别 -->
          <template v-for="item2 in item.children">
            <el-menu-item :index="item2.path">
              <el-icon>
                <component :is="item2.meta.icon"></component>
              </el-icon>
              {{ item2.label }}
            </el-menu-item>
          </template>
        </el-sub-menu>
        <el-menu-item v-else :index="item.path">
          <el-icon>
            <component :is="item.meta.icon"></component>
          </el-icon>
          <span> {{ item.label }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { userMainStore } from '@/store/';
const store = userMainStore();
const router = useRouter();
const route = useRoute();
const isCollapse = computed(() => !(store.asideWidth == '250px'));
// 默认选中
const defaultActive = ref(route.path);
const asideMenus: Array<any> = store.menuTree;
const handleSelect = (e: any) => {
  router.push(e);
};
</script>

<style scoped lang="scss">
.f-menu {
  transition: all 0.2s;
  top: 64px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.f-menu .el-menu {
  background: transparent;
  border-right: none;
  height: 100%;
}

.f-menu .el-menu-item,
.f-menu .el-sub-menu__title {
  color: #495057;
  font-weight: 500;
  transition: all 0.3s ease;
  border-radius: 8px;
  margin: 4px 8px;
  width: calc(100% - 16px);
}

.f-menu .el-menu-item:hover,
.f-menu .el-sub-menu__title:hover {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: translateX(4px);
}

.f-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-weight: 600;
}

.f-menu .el-sub-menu .el-menu-item {
  margin-left: 16px;
  width: calc(100% - 32px);
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.f-menu .el-sub-menu .el-menu-item:hover {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: translateX(4px);
}

.f-menu .el-sub-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-weight: 600;
}

.f-menu .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

/* 折叠状态下的样式 */
.f-menu .el-menu--collapse .el-menu-item,
.f-menu .el-menu--collapse .el-sub-menu__title {
  margin: 4px 4px;
  width: calc(100% - 8px);
  text-align: center;
}

.f-menu .el-menu--collapse .el-sub-menu .el-menu-item {
  margin-left: 8px;
  width: calc(100% - 16px);
}

/* 滚动条样式 */
.f-menu::-webkit-scrollbar {
  width: 6px;
}

.f-menu::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;
}

.f-menu::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 3px;
}

.f-menu::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
}
</style>
